package sentinel.demo03.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import sentinel.demo03.handler.MyBlockHandler;
import sentinel.demo03.handler.MyFallback;
import sentinel.demo03.service.HelloService;

import java.util.Date;

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    @SentinelResource(
            value="sayHi01",
            blockHandler = "sayHiError",
            blockHandlerClass = MyBlockHandler.class,
            fallback = "sayHiThrow",
            fallbackClass = MyFallback.class
    )
    public String sayHi(String name) throws InterruptedException {
        Long start= new Date().getTime();
        //将helloService.sayHi(name) 看成是资源来定义
        String result=null;
        System.out.println(helloService.getClass().getName());
        result=helloService.sayHi(name);
        Long end= new Date().getTime();
        Thread.sleep(100);
        System.out.println("当前sayHi执行时间:"+(start-end));
        return result;
    }
    /*@SentinelResource(value="aaa")
    public void test(){
        System.out.println("haha");
    }*/
}
